package ystp.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Ysxiangmu;
import ystp.service.YsxiangmuService;
import ystp.support.JsonResult;
import ystp.support.PageInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("xiangmu")
public class YsxiangmuController {
    @Resource
    YsxiangmuService ddService;

    public Logger log = LoggerFactory.getLogger(YsxiangmuController.class);

    @RequestMapping("findAllBypage")
    public JsonResult<?> findVaildByAdminId(@RequestParam("xiangmuname") String ppXiangmuName,
                                            @RequestParam("pageindex") int ppPageIndex,
                                            @RequestParam("pagesize") int ppPageSize){
        try {
            if(!ppXiangmuName.equals("")) {
                ppXiangmuName = "%" + ppXiangmuName + "%";
            }
            List<Map<String, Object>> mmList=ddService.findAllBypage(ppXiangmuName,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getCount(ppXiangmuName);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("XiangMuList", mmList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/findAllBypage:error",e);
            return JsonResult.getErrorResult("xiangmu/findAllBypage:error " + e.getMessage());
        }
    }

    @RequestMapping("add")
    public JsonResult<?> add(@RequestParam("xiangmuname") String ppxiangmuname,
                             @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                          /*   @RequestParam("meetingtime") String ppmeetingtime,*/
                             @RequestParam("zhuangjiashu") int ppzhuangjiashu,
                             @RequestParam("tongguoshu") int pptongguoshu,
                             HttpServletRequest request){
        try {

            SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd");

           /* Date mmmeetingtime=simdate.parse(ppmeetingtime);*/

            Ysxiangmu mmYsxiangmu=new Ysxiangmu();
            mmYsxiangmu.setXiangmuid(UUID.randomUUID().toString());
            mmYsxiangmu.setXiangmuname(ppxiangmuname);
            mmYsxiangmu.setXiangmuleixing(ppxiangmuleixing);
            mmYsxiangmu.setZhuangjiashu(ppzhuangjiashu);
            mmYsxiangmu.setMeetingtime(new Date());
            mmYsxiangmu.setTongguoshu(pptongguoshu);
            /* mmYsxiangmu.setMeetingtime(mmmeetingtime);*/
            mmYsxiangmu.setStatus(100);
            mmYsxiangmu.setCreatetime(new Date());
            mmYsxiangmu.setIsactived(0);
            mmYsxiangmu.setLunci(1);
            HttpSession session = request.getSession();
            mmYsxiangmu.setCreatetime(new Date());
            mmYsxiangmu.setCreateby(session.getAttribute("Id").toString());

            return ddService.add(mmYsxiangmu)==true? JsonResult.getSuccessResult("新增项目成功")
                    : JsonResult.getErrorResult("新增项目失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/add:error",e);
            return JsonResult.getErrorResult("xiangmu/add:error " + e.getMessage());
        }

    }
    @RequestMapping("find_one")
    public JsonResult<?> find_one(@RequestParam("xianmguid") String ppxianmguId) {
        try {
            return JsonResult.getSuccessResult(ddService.findOne(ppxianmguId));

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/find_one:error",e);
            return JsonResult.getErrorResult("xiangmu/find_one:error " + e.getMessage());
        }
    }

    @RequestMapping("getIsactivedxiangmu")
    public JsonResult<?> getIsactivedxiangmu(){
        try {
            return JsonResult.getSuccessResult(ddService.getIsactivedxiangmu());
        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/getIsactivedxiangmu:error",e);
            return JsonResult.getErrorResult("xiangmu/getIsactivedxiangmu:error " + e.getMessage());
        }
    }

    @RequestMapping("modify")
    public JsonResult<?> modify(@RequestParam("xianmguid") String ppxianmguId,
                                @RequestParam("xiangmuname") String ppxiangmuname,
                                @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                              /*  @RequestParam("meetingtime") String ppmeetingtime,*/
                                @RequestParam("zhuangjiashu") int ppzhuangjiashu,
                                @RequestParam("tongguoshu") int pptongguoshu,
                                HttpServletRequest request){
        try {
            SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd");

           /* Date mmmeetingtime=simdate.parse(ppmeetingtime);*/
            Ysxiangmu mmYsxiangmu=new Ysxiangmu();
            mmYsxiangmu.setXiangmuid(ppxianmguId);
            mmYsxiangmu.setXiangmuname(ppxiangmuname);
            mmYsxiangmu.setXiangmuleixing(ppxiangmuleixing);
            mmYsxiangmu.setZhuangjiashu(ppzhuangjiashu);
            mmYsxiangmu.setTongguoshu(pptongguoshu);
            /*mmYsxiangmu.setMeetingtime(mmmeetingtime);*/
            mmYsxiangmu.setUpdatetime(new Date());
            HttpSession session = request.getSession();
            mmYsxiangmu.setCreateby(session.getAttribute("Id").toString());
            return ddService.modify(mmYsxiangmu)==true? JsonResult.getSuccessResult("修改项目成功")
                    : JsonResult.getErrorResult("修改项目失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/modify:error",e);
            return JsonResult.getErrorResult("xiangmu/modify:error " + e.getMessage());
        }
    }

    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xianmguid") String ppxianmguId){
        try {
            Ysxiangmu mmYsxiangmu=new Ysxiangmu();
            mmYsxiangmu.setXiangmuid(ppxianmguId);
            mmYsxiangmu.setStatus(200);
            return ddService.modify(mmYsxiangmu)==true? JsonResult.getSuccessResult("删除项目成功")
                    : JsonResult.getErrorResult("删除项目失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/delete:error",e);
            return JsonResult.getErrorResult("xiangmu/delete:error " + e.getMessage());
        }
    }

    @RequestMapping("modifyisactived")
    public JsonResult<?> modifyisactive(@RequestParam("xiangmuid") String ppxiangmuId,
                                        @RequestParam("isactive") int ppisactive){
        try {

            if(ppisactive==1){
                if(!ddService.modifynoactived()){
                    JsonResult.getErrorResult("修改失败");
                }
                return ddService.modifyisactived(ppxiangmuId,ppisactive) == true ? JsonResult.getSuccessResult("修改成功")
                        : JsonResult.getErrorResult("修改失败");
            }else{
                return ddService.modifyisactived(ppxiangmuId,ppisactive) == true ? JsonResult.getSuccessResult("修改成功")
                        : JsonResult.getErrorResult("修改失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/modifyisactived:error",e);
            return JsonResult.getErrorResult("xiangmu/modifyisactived:error " + e.getMessage());
        }
    }

    @RequestMapping("nextlunci")
    public JsonResult<?> nextlunci(@RequestParam("xiangmuid") String ppxiangmuId){
        try {
            Ysxiangmu mmYsxiangmu = ddService.findOne(ppxiangmuId);
            if (mmYsxiangmu==null){
                return JsonResult.getErrorResult("无该项目");
            }
            mmYsxiangmu.setLunci(mmYsxiangmu.getLunci()+1);

            return ddService.modify(mmYsxiangmu) == true ? JsonResult.getSuccessResult("修改成功")
                    : JsonResult.getErrorResult("修改失败");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("xiangmu/nextlunci:error",e);
            return JsonResult.getErrorResult("xiangmu/nextlunci:error " + e.getMessage());
        }
    }

}
